﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
	public partial class TraCuu
	{
        private InsertInfo m_insertInfo = new InsertInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private DeleteInfo m_deleteInfo = new DeleteInfo();
        private SelectInfo m_selectInfo = new SelectInfo();
        private ShowInfo m_showInfo = new ShowInfo();
        private XuLyThem m_XuLyKhac = new XuLyThem();

        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        private bool m_bSelected;


		public TraCuu()
		{
			this.InitializeComponent();
            addCmbTimTheo();
			// Insert code required on object creation below this point.
		}

        private void ClearIndex()
        {
            txtTSTacGia.Text = "";
            txtTSMaSach.Text = "";
            txtTSTenSach.Text = "";
            txtTSTheLoai.Text = "";
            txtTSNhaXuatBan.Text = "";
        }

        private void addCmbTimTheo()
        {
            CmbTimTheo.Items.Add("Tên tác giả");
            CmbTimTheo.Items.Add("Tên Sách");
            CmbTimTheo.Items.Add("Nhà xuất bản");
            CmbTimTheo.Items.Add("Thể loại");
        }

        private void bntTimTiep_Click(object sender, RoutedEventArgs e)
        {
            switch (CmbTimTheo.SelectedItem.ToString())
            {
                case "Tên tác giả":
                    m_bSelected = false;
                    m_selectInfo.SelectSach_TacGia(dataTraSach, txtTuKhoa.Text);
                    break;
                case "Tên Sách":
                    m_bSelected = false;
                    m_selectInfo.SelectSach_TenDauSach(dataTraSach, txtTuKhoa.Text);
                    break;
                case "Nhà xuất bản":
                    m_bSelected = false;
                    m_selectInfo.SelectSach_NhaXuatBan(dataTraSach, txtTuKhoa.Text);
                    break;
                case "Thể loại":
                    m_bSelected = false;
                    m_selectInfo.SelectSach_TheLoai(dataTraSach, txtTuKhoa.Text);
                    break;
            }            
        }

        private void dataTraSach_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelected)
            {
                var item = dataTraSach.SelectedItem as DSTimSach;
                txtTSTacGia.Text = item.HoTenTacGia;
                txtTSMaSach.Text = item.MaSach;
                txtTSTenSach.Text = item.TenDauSach;
                txtTSTheLoai.Text = item.TenTheLoai;
                txtTSNhaXuatBan.Text = item.NhaXuatBan;
            }
            else
            {
                m_bSelected = true;
                dataTraSach.ItemsSource = null;
            }
        }

        private void CmbTimTheo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ClearIndex();
            txtTuKhoa.Text = "";
            m_bSelected = true;
            dataTraSach.ItemsSource = null;
        }

        private void txtTuKhoa_TextChanged(object sender, TextChangedEventArgs e)
        {
            ClearIndex();
            m_bSelected = true;
            dataTraSach.ItemsSource = null;
        }
	}
}